﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Reflection;

namespace SvnNotes
{
    class Program
    {
        static void Main(string[] args)
        {
            new SvnNotesProgram(args).Run();
        }
    }

    public class SvnNotesProgram
    {
        private string[] _Args = null;
        public SvnNotesProgram(string[] args)
        {
            _Args = args;
        }

        public void Run()
        {
            if (_Args.Length == 0 || _Args[0] == "/help")
            {
                ShowHelp();
                return;
            }

             Arg transformerArg = GetArg("/transformer");
             if (transformerArg != null)
             {
                 if (String.IsNullOrEmpty(transformerArg.Value))
                 {
                     Console.WriteLine("ERROR: /transformer has no value");
                     return;
                 }

                 Regex r = new Regex(@"^(?<type>\S+)(\s+)?,(\s+)?(?<assemblyfile>.*)$");

                Match m = r.Match(transformerArg.Value);
                if (m.Success)
                {
                    IOutputTransformer transformer = OutputTransformerFactory.GetOutputTransformer(m.Groups["assemblyfile"].Value, m.Groups["type"].Value);
                    SvnNotesFile.Create(Console.OpenStandardInput(), _Args[0], transformer, _Args);
                }
             }
             else
             {
                 SvnNotesFile.Create(Console.OpenStandardInput(), _Args[0], _Args);

             }
        }

        public void ShowHelp()
        {
            Console.WriteLine("usage: svnnotes filename [options] [args]");
            Console.WriteLine();
            Console.WriteLine("/help:\t\tDisplays this help message");
            Console.WriteLine("/transformer:\tThe output transformer to use");
            Console.WriteLine("\t\tin the format \"fulltypename, assemblyfilename\"");
            Console.WriteLine();
            Console.WriteLine("examples:");
            Console.WriteLine("svn log --xml --revision 4365:HEAD | svnnotes log.txt");
            Console.WriteLine("svn log --xml --revision 4365:HEAD | svnnotes log.txt /transformer \"Foo.Bar.MyTranformer, Foo.Bar.dll\"");
        }

        public Arg GetArg(string argName)
        {
            var arg = _Args.Select((item, index) => new Arg(item, index))
                    .Where(x => x.Name == argName).FirstOrDefault();

            if (arg != null)
            {
                if (arg.ArgIndex + 1 < _Args.Length)
                {
                    arg.Value = _Args[arg.ArgIndex + 1];
                }
            }
            return arg;
        }
    }

    public class Arg
    {
        public string Name { get; set; }
        public int ArgIndex { get; set; }
        public string Value { get; set; }
        public Arg(string name, int argIndex)
        {
            Name = name;
            ArgIndex = argIndex;
        }
    }
}
